package com.thz.friend.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.thz.common.core.constants.Constants;
import com.thz.common.core.utlis.ThreadLocalUtil;
import com.thz.friend.mapper.course.CourseMapper;
import com.thz.friend.mapper.registration.CourseRegistrationMapper;
import com.thz.friend.mapper.report.ReportMapper;
import com.thz.friend.mapper.sysuser.SysUserMapper;
import com.thz.friend.pojo.course.Course;
import com.thz.friend.pojo.course.vo.CourseVO;
import com.thz.friend.pojo.registration.CourseRegistration;
import com.thz.friend.pojo.report.Report;
import com.thz.friend.pojo.sysuser.SysUser;
import com.thz.friend.service.course.ICourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author thz
 * @Date 2025/5/10
 */
@Service
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private CourseRegistrationMapper courseRegistrationMapper;

    @Override
    public List<Course> list() {
        List<Course> courseList = courseMapper.selectList(null);
        Long studentId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        for (Course course : courseList) {
            CourseRegistration courseRegistration = courseRegistrationMapper.selectOne(new LambdaQueryWrapper<CourseRegistration>()
                    .eq(CourseRegistration::getCourseId, course.getCourseId()).eq(CourseRegistration::getStudentId, studentId));
            Boolean type = courseRegistration != null;
            course.setType(type);
        }
        return courseList;
    }

    @Override
    public CourseVO selectCourseVOById(Long id) {

//        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);

//        Report reports = reportMapper.getReports(id,userId);
//
//        if (reports == null) {
//            Report report = new Report();
//            report.setCourseId(id);
//            reportMapper.insert(report);
//        }

        Course course = courseMapper.selectById(id);
        CourseVO courseVO = new CourseVO();
        BeanUtils.copyProperties(course, courseVO);
        SysUser sysUser = sysUserMapper.selectById(course.getCreateUser());
        courseVO.setTeacherName(sysUser.getNickName());
        return courseVO;
    }

}
